package com.blog.dao.impl;

import com.blog.dao.MessageDao;
import com.blog.pojo.Message;
import com.blog.util.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

public class MessageDaoImpl implements MessageDao {
    private QueryRunner queryRunner = new QueryRunner( DruidUtils.getDataSource());
   //添加消息
    @Override
    public int  addMessage(Message message) {
        String sql = "insert into message(senderId,receiverId,content) values(?,?,?)";
        Object[] params = {message.getSenderId(),message.getReceiverId(),message.getContent()};
        try {
           int   add = queryRunner.update(sql, params);
            return add;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }

    //删除消息
    @Override
    public int deleteMessage(int id) {
        String sql = "delete from message where id = ?";
        try {
            int delete = queryRunner.update(sql, id);
            return delete;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
//查询已发送的消息
    @Override
    public List<Message> findMessageBySenderId(int senderId) {
        String sql = "select * from message where senderId = ?";
        Object[] params = {senderId};
        try {
            List<Message> messages = queryRunner.query(sql, new BeanListHandler<Message>(Message.class), params);
            return messages;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
//查询已接收的消息
    @Override
    public List<Message> findMessageByReceiveId(int receiverId) {
        String sql = "select * from message where receiverId = ?";
        Object[] params = {receiverId};
        try {
            List<Message> messages = queryRunner.query(sql, new BeanListHandler<Message>(Message.class), params);
            return messages;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
//判断是否已经单向发过一条信息
    @Override
    public boolean countSenderRecevier(int senderId, int receiveId) {
        String sql="SELECT COUNT(*) >0 FROM message where senderId=? AND receiverId =?";
        Object[] params = {senderId,receiveId};
        try {
            long count = queryRunner.query(sql, new ScalarHandler<>(), params);
            if(count>0){
                return true;
            }
            return false;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
//查询 senderId和receiveId之间的全部消息
    @Override
    public List<Message> findMessageBySenderAndReceiver(int senderId, int receiveId) {
        String sql="SELECT * from message where (senderId=? AND receiverId =?) or (senderId=? AND receiverId =?) ORDER BY sentTime";
        Object[] params = {senderId,receiveId,receiveId,senderId};

        try {
            List<Message> messagesList = queryRunner.query(sql, new BeanListHandler<Message>(Message.class), params);
            return messagesList;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
//判断 senderId和receiveId之间是否已经关注
    @Override
    public long followeeSenderReceiver(int senderId, int receiveId) {
        String sql="SELECT COUNT(*) From followee where (uId=? AND followeeId=?) or (uId=? AND followeeId=?)";
        Object[] params = {senderId,receiveId,receiveId,senderId};
        try {
            long count = queryRunner.query(sql, new ScalarHandler<>(), params);
            return  count;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
//标注已读
    @Override
    public int updateMessage(int id, byte isRead) {
        String sql ="UPDATE message set isRead=1 where id=?";
        try {
            int update = queryRunner.update(sql, id);
            return update;
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public Message findById(int id) {
        String sql ="SELECT * FROM message where id=?";
       Object[] params = {id};
       try {
           Message message = queryRunner.query(sql, params, new BeanHandler<Message>(Message.class));
           return message;
       } catch (SQLException e) {
           throw new RuntimeException(e);
       }
    }


}
